package cn.innosmart.aq.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.tutk.p2p.ConnectionEntity;
import com.tutk.p2p.ConnectionManager;

import org.json.JSONException;
import org.json.JSONObject;

import cn.innosmart.aq.R;
import cn.innosmart.aq.application.AQApplication;
import cn.innosmart.aq.bean.BoxBean;
import cn.innosmart.aq.customize.CustomsizeDialog;
import cn.innosmart.aq.customize.ModifyPasswordDialog;
import cn.innosmart.aq.customize.MyDialog;
import cn.innosmart.aq.customize.RoundProgressBar;
import cn.innosmart.aq.manager.SystemManager;
import cn.innosmart.aq.util.INetworkInteractUtil;
import cn.innosmart.aq.util.NetworkInteractUtil;
import cn.innosmart.aq.util.SystemConstant;
import cn.innosmart.aq.view.activity.base.BaseActivity;

/**
 * Created by David on 2015/9/1.
 */
public class HubSettingActivity extends BaseActivity implements INetworkInteractUtil {
    private static Handler mHandler;
    private final int EXIT = 0;
    private final int ALLCHANGE = 1;
    private final int SHOWPROGRESSDIALOG = 2;
    private final int CHANGEPROGRESSDIALOG = 3;
    private final int MD5MATCHRESULT = 4;
    private final int RESET = 1;
    private final int REBOOT = 3;
    private final int UPDATESUCCESS = 2;
    private ConnectionEntity connectionEntity;
    private RelativeLayout rlHubUpdate;
    private RelativeLayout rlReboot;
    private ImageView ivMoreArrow3;
    private TextView tvVersionName;
    private RelativeLayout rlStorageInfo;
    private RelativeLayout rlModifyPassword;
    private TextView tvStorageInfo;
    private ImageView ivMoreArrow1;
    private TextView tvStorage;
    private RelativeLayout rlMemoryInfo;
    private ImageView ivMoreArrow2;
    private TextView tvFlash;
    private RelativeLayout rlReset;
    private ImageView ivMoreArrow4;
    private String hubVersion = null;
    private String storageUsed = null;
    private String storageAll = null;
    private String storageUsedPercent = null;
    private String flashUsed = null;
    private String flashAll = null;
    private String flashUsedPercent = null;
    private NetworkInteractUtil networkInteractUtil;
    /**
     * 设置界面Toolbar
     */
    private Toolbar toolbar;
    private CustomsizeDialog hubUpdate;
    private JsonObjectRequest jsonUpdateRequest;
    private String updateurl = "http://fwupgrade.gangganghao.com.cn/download_aq_01/palals.json";
    //    http://54.251.116.21/download_aq_01/palals.json
//    http://54.251.116.21/download_aq_01_debug/palals.json
    private MyDialog myDialog;
    private Button bt_positive;
    private Button bt_negative;
    private Button bt_cancel;
    private TextView tv_content;
    private RelativeLayout rl_cancel;
    private RoundProgressBar mProgress;
    private ModifyPasswordDialog modifyPasswordDialog;
    private View.OnClickListener mBtnOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.rl_modify_password:
                    showModifyPasswordDialog();
                    break;
                case R.id.rl_reboot:
                    showReboot();
                    break;
                case R.id.rl_memory_info:
                    loadingDialog();
                    SystemManager.getInstance().QueryFlashInfo(connectionEntity, new SystemManager.QueryFlashInfoCallBack() {
                        @Override
                        public void onQueryFlashInfoResponseCallBack(int ret, JSONObject info) {
                            if (ret == 0) {
                                try {
                                    flashUsed = info.getString("used");
                                    flashAll = info.getString("total");
                                    flashUsedPercent = String.valueOf((Double.valueOf(flashUsed) / Double.valueOf(flashAll) * 100)) + "%";
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                            hideloadingDialog();
                            mHandler.sendEmptyMessage(ALLCHANGE);
                        }
                    });
                    break;
                case R.id.rl_reset:
                    showResetSureDialog();
                    break;
                case R.id.rl_hub_update:
                    checkHubUpdate();
                    break;
                case R.id.rl_storage_info:
                    loadingDialog();
                    SystemManager.getInstance().QueryStorageInfo(connectionEntity, new SystemManager.QueryStorageInfoCallBack() {
                        @Override
                        public void onQueryStorageInfoResponseCallBack(int ret, JSONObject info) {
                            if (ret == 0) {
                                if (ret == 0) {
                                    try {
                                        storageUsed = info.getString("Used");
                                        storageAll = info.getString("Total");
                                        storageUsedPercent = info.getString("UsePercent");
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                            hideloadingDialog();
                            mHandler.sendEmptyMessage(ALLCHANGE);
                        }
                    });
                    break;
            }

        }
    };

    public static Handler getHandler() {
        return mHandler;
    }

    private void showReboot() {
        final CustomsizeDialog customsizeDialog = new CustomsizeDialog(this);
        customsizeDialog.setTitle(R.string.dialog_title_alert);
        customsizeDialog.setCancelable(false);
        customsizeDialog.setContent(getString(R.string.reboot_sure));
        customsizeDialog.setPositiveButton(R.string.sure, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customsizeDialog.dismiss();
                JSONObject jsonParams = new JSONObject();
                try {
                    jsonParams.put("cmdline", "reboot");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                loadingDialog();
                SystemManager.getInstance().ExecuteShellScript(connectionEntity, jsonParams.toString(), new SystemManager.onReboot() {
                    @Override
                    public void onRebootCallBack(int ret) {
                        hideloadingDialog();
                        if (ret == 0) {
                            exit();
                        } else if (ret == -1) {
                            showToast(getString(R.string.dialog_action_error));
                        }
                    }
                });
            }
        });
        customsizeDialog.setNegativeButton(R.string.cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customsizeDialog.dismiss();
            }
        });
        customsizeDialog.show();
    }

    public void showModifyPasswordDialog() {
        modifyPasswordDialog = new ModifyPasswordDialog(this);
        final EditText etOldPassword = (EditText) modifyPasswordDialog.findViewById(R.id.et_old_password);
//        modifyPasswordDialog.findViewById(R.id.ll_old_password).setVisibility(View.GONE);
        final EditText etNewPassword = (EditText) modifyPasswordDialog.findViewById(R.id.et_new_password);
        final EditText etNewPasswordSure = (EditText) modifyPasswordDialog.findViewById(R.id.et_new_password_sure);
        modifyPasswordDialog.setTitle(R.string.dialog_title_alert);
        modifyPasswordDialog.setPositiveButton(getString(R.string.sure), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPassword = etOldPassword.getText().toString().trim();
                final String newPassword = etNewPassword.getText().toString().trim();
                String newPasswordSure = etNewPasswordSure.getText().toString().trim();
                if (TextUtils.isEmpty(oldPassword)) {
                    showToast(getString(R.string.activity_countsetting_old_password_can_not_empty));
                    YoYo.with(Techniques.Shake).playOn(etOldPassword);
                    return;
                }
                if (!oldPassword.equals(connectionEntity.getPassword())) {
                    showToast(getString(R.string.activity_countsetting_old_password_wrong));
                    YoYo.with(Techniques.Shake).playOn(etOldPassword);
                    return;
                }
                if (TextUtils.isEmpty(newPassword)) {
                    showToast(getString(R.string.activity_countsetting_input_new_password));
                    YoYo.with(Techniques.Shake).playOn(etNewPassword);
                    return;
                }
                if (TextUtils.isEmpty(newPasswordSure)) {
                    showToast(getString(R.string.activity_countsetting_sure_new_password));
                    YoYo.with(Techniques.Shake).playOn(etNewPasswordSure);
                    return;
                }
                if (!newPassword.equals(newPasswordSure)) {
                    showToast(getString(R.string.activity_countsetting_twice_password_not_same));
                    YoYo.with(Techniques.Shake).playOn(etNewPassword);
                    YoYo.with(Techniques.Shake).playOn(etNewPasswordSure);
                    return;
                }
                JSONObject jsParam = new JSONObject();
                try {
                    jsParam.put("username", "admin");
                    jsParam.put("password", newPassword);
                    jsParam.put("permission", "10");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                loadingDialog();
                SystemManager.getInstance().UpdateUser(connectionEntity, jsParam.toString(), new SystemManager.ModifyPassword() {
                    @Override
                    public void onModifyPasswordCallBack(int ret) {
                        hideloadingDialog();
                        if (ret == 0) {
                            String uid = connectionEntity.getUid();
                            BoxBean modifyBoxBean = null;
                            for (BoxBean boxBean : SystemConstant.boxBeanList) {
                                if (boxBean.getUid().equals(uid)) {
                                    modifyBoxBean = boxBean;
                                    break;
                                }
                            }
                            modifyBoxBean.setPassword(newPassword);
                            if (modifyBoxBean != null) {
                                networkInteractUtil.syncModifyBox(modifyBoxBean.getUid(), modifyBoxBean.getModifyInfo());
                            }
                        } else {
                            showToast(getString(R.string.modify_failed));
                        }
                    }
                });
            }
        });
        modifyPasswordDialog.setNegativeButton(getString(R.string.dialog_btn_cancel), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyPasswordDialog.dismiss();
            }
        });
        modifyPasswordDialog.setCanceledOnTouchOutside(false);
        modifyPasswordDialog.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hub_setting);
        mHandler = new UiHandler();
        setBar();
        connectionEntity = ConnectionManager.getInstance().getCurrentConnectionEntity();
        networkInteractUtil = new NetworkInteractUtil(this);
        initData();
        assignViews();

        checkHubUpdate();
    }

    private void assignViews() {
        rlHubUpdate = (RelativeLayout) findViewById(R.id.rl_hub_update);
        rlReboot = (RelativeLayout) findViewById(R.id.rl_reboot);
        ivMoreArrow3 = (ImageView) findViewById(R.id.iv_more_arrow_3);
        tvVersionName = (TextView) findViewById(R.id.tv_versionName);
        rlStorageInfo = (RelativeLayout) findViewById(R.id.rl_storage_info);
        rlModifyPassword = (RelativeLayout) findViewById(R.id.rl_modify_password);
        tvStorageInfo = (TextView) findViewById(R.id.tv_storage_info);
        ivMoreArrow1 = (ImageView) findViewById(R.id.iv_more_arrow_1);
        tvStorage = (TextView) findViewById(R.id.tv_storage);
        rlMemoryInfo = (RelativeLayout) findViewById(R.id.rl_memory_info);
        ivMoreArrow2 = (ImageView) findViewById(R.id.iv_more_arrow_2);
        tvFlash = (TextView) findViewById(R.id.tv_flash);
        rlReset = (RelativeLayout) findViewById(R.id.rl_reset);
        ivMoreArrow4 = (ImageView) findViewById(R.id.iv_more_arrow_4);
        rlMemoryInfo.setOnClickListener(mBtnOnClickListener);
        rlReboot.setOnClickListener(mBtnOnClickListener);
        rlReset.setOnClickListener(mBtnOnClickListener);
        rlHubUpdate.setOnClickListener(mBtnOnClickListener);
        rlStorageInfo.setOnClickListener(mBtnOnClickListener);
        rlModifyPassword.setOnClickListener(mBtnOnClickListener);
    }

    private void initData() {
        loadingDialog();
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemManager.getInstance().GetInfo(connectionEntity, new SystemManager.GetInfoCallBack() {
                    @Override
                    public void onGetInfoResponseCallBack(int ret, String version) {
                        if (ret == 0) {
                            hubVersion = version;
                        }
                    }
                });
                //{"total":"59520","used":"24836","free":"34684","shared":"0"}
                SystemManager.getInstance().QueryFlashInfo(connectionEntity, new SystemManager.QueryFlashInfoCallBack() {
                    @Override
                    public void onQueryFlashInfoResponseCallBack(int ret, JSONObject info) {
                        if (ret == 0) {
                            try {
                                flashUsed = info.getString("used");
                                flashAll = info.getString("total");
                                flashUsedPercent = String.valueOf(Integer.valueOf(flashUsed) / Integer.valueOf(flashAll));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                //{"output":{"Filesystem":"/dev/mtdblock5","Total":"12224","Used":"2944", "Available":"9280","UsePercent":"24%"}
                SystemManager.getInstance().QueryStorageInfo(connectionEntity, new SystemManager.QueryStorageInfoCallBack() {
                    @Override
                    public void onQueryStorageInfoResponseCallBack(int ret, JSONObject info) {
                        if (ret == 0) {
                            if (ret == 0) {
                                try {
                                    storageUsed = info.getString("Used");
                                    storageAll = info.getString("Total");
                                    storageUsedPercent = info.getString("UsePercent");
                                    ;
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        hideloadingDialog();
                        mHandler.sendEmptyMessage(ALLCHANGE);
                    }
                });
            }
        }).start();

    }

    private void setBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ((ImageView) findViewById(R.id.iv_headicon)).setImageResource(R.drawable.toolbar_back);
        toolbar.setTitle("");
        ((TextView) findViewById(R.id.tv_toolbar)).setText(getString(R.string.activity_hub_setting_title));
        setSupportActionBar(toolbar);
        findViewById(R.id.iv_headicon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mDrawer.openDrawer(GravityCompat.START);
                mHandler.sendEmptyMessage(EXIT);
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void showResetSureDialog() {
        final CustomsizeDialog customsizeDialog = new CustomsizeDialog(this);
        customsizeDialog.setTitle(R.string.dialog_title_alert);
        customsizeDialog.setContent(getString(R.string.activity_hub_setting_reset));
        customsizeDialog.setNegativeButton(R.string.cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customsizeDialog.dismiss();
            }
        });
        customsizeDialog.setPositiveButton(R.string.sure, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hubRest();
                customsizeDialog.dismiss();
            }
        });
        customsizeDialog.show();
    }

    private void showHubUpdateSureDialog(final String url, final String verison, final String md5, int clear) {
        if (hubUpdate == null) {
            hubUpdate = new CustomsizeDialog(this);
            hubUpdate.setTitle(R.string.dialog_title_alert);
            hubUpdate.setCancelable(false);
            hubUpdate.setCanceledOnTouchOutside(false);
            if (clear == 0)
                hubUpdate.setContent(getString(R.string.activity_hub_setting_new_firmware_sure));
            else if(clear==1)
                hubUpdate.setContent(getString(R.string.activity_hub_setting_new_firmware_sure)+getString(R.string.update_clear_datas));
            hubUpdate.setNegativeButton(R.string.cancel, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hubUpdate.dismiss();
                }
            });
            hubUpdate.setPositiveButton(R.string.sure, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hubUpdate(url, verison, md5);
                    hubUpdate.dismiss();
                }
            });
        }
        if (!hubUpdate.isShowing()) {
            hubUpdate.show();
        }
    }

    private void hubRest() {
        SystemManager.getInstance().Reset(connectionEntity, new SystemManager.ResetCallBack() {
            @Override
            public void onResetCallBack(int ret, JSONObject info) {

            }
        });
        setResult(RESET);
        finish();
    }

    @Override
    protected void onResume() {
        setBaseDialogContext(this);
        super.onResume();
        toolbar.setNavigationIcon(null);
    }

    private void checkHubUpdate() {
        jsonUpdateRequest = new JsonObjectRequest(updateurl, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("TAG", response.toString());
                        dealResponse(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("TAG", error.getMessage(), error);
                    }
                });

        AQApplication.getInstance().mQueue.add(jsonUpdateRequest);
    }

    //{"url":"http:\/\/54.251.116.21\/download_aq_01_debug\/palals.tgz","version":"V15.08.31","md5":"3013b2f6074c72e85af07452d8f11235"}
    private void dealResponse(JSONObject response) {
        try {
            String version = response.getString("version");
            String url = response.getString("url");
            String md5 = response.getString("md5");
            int clear = 0;
            try {
                clear = response.getInt("clear");
            } catch (Exception e) {

            }

            String newVersion = version.replace("V", "");
            String[] newVersions = newVersion.split("\\.");
            int newT = Integer.valueOf(newVersions[0]);
            int newD = Integer.valueOf(newVersions[1]);
            int newU = Integer.valueOf(newVersions[2]);
            String oldVersion = hubVersion.replace("V", "");
            String[] oldVersions = oldVersion.split("\\.");
            int oldT = Integer.valueOf(oldVersions[0]);
            int oldD = Integer.valueOf(oldVersions[1]);
            int oldU = Integer.valueOf(oldVersions[2]);

            if (newT <= oldT) {
                if (newD <= oldD) {
                    if (newU <= oldU) {
                        showToast(getString(R.string.this_is_newest_harmware_version));
                    } else {
                        showHubUpdateSureDialog(url, version, md5,clear);
                    }
                } else {
                    showHubUpdateSureDialog(url, version, md5,clear);
                }
            } else {
                showHubUpdateSureDialog(url, version, md5,clear);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void hubUpdate(String url, String version, String md5) {
        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("version", version);
            jsonParams.put("url", url);
            jsonParams.put("md5", md5);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        loadingDialog();
        SystemManager.getInstance().DownloadFirmware(connectionEntity, jsonParams.toString(), new SystemManager.DownloadCallBack() {
            @Override
            public void onDownloadCallBack(int ret) {
                if (ret == 0) {
                    hideloadingDialog();
                    mHandler.sendEmptyMessage(SHOWPROGRESSDIALOG);
                }
            }
        });
    }

    private void alert() {
        myDialog = new MyDialog(this);
        myDialog.setTitle(getString(R.string.activity_hub_setting_firmware_downloading));
        myDialog.setCancelable(false);
        myDialog.setCanceledOnTouchOutside(false);
        mProgress = (RoundProgressBar) myDialog.findViewById(R.id.rd_pb);
        mProgress.setVisibility(View.VISIBLE);
        bt_positive = (Button) myDialog.findViewById(R.id.bt_positive);
        bt_negative = (Button) myDialog.findViewById(R.id.bt_negative);
        bt_negative.setVisibility(View.GONE);
        bt_positive.setVisibility(View.GONE);
        rl_cancel = (RelativeLayout) myDialog.findViewById(R.id.rl_cancel);
        bt_cancel = (Button) myDialog.findViewById(R.id.bt_canceldownload);
        tv_content = (TextView) myDialog.findViewById(R.id.tv_content);
        tv_content.setVisibility(View.GONE);
        rl_cancel.setVisibility(View.GONE);
        myDialog.show();
    }

    private void showUpdateConFirmDialog() {
        final CustomsizeDialog customsizeDialog = new CustomsizeDialog(this);
        customsizeDialog.setTitle(R.string.dialog_title_alert);
        customsizeDialog.setContent(getString(R.string.activity_hub_setting_firmware_upgrade_sure));
        customsizeDialog.setCancelable(false);
        customsizeDialog.setPositiveButton(R.string.sure, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmUpdate("upgrade");
                customsizeDialog.dismiss();
            }
        });
        customsizeDialog.setNegativeButton(R.string.cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmUpdate("discard");
                customsizeDialog.dismiss();
            }
        });
        customsizeDialog.setCanceledOnTouchOutside(false);
        customsizeDialog.show();
    }

    private void confirmUpdate(String operate) {
        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("operate", operate);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        SystemManager.getInstance().ConfirmUpgradeFirmware(connectionEntity, jsonParams.toString(), new SystemManager.ConfirmCallBack() {
            @Override
            public void onConfirmCallBack(int ret) {
                if (ret == 0) {
                }
            }
        });
        if (operate.equals("upgrade")) {
            setResult(UPDATESUCCESS);
            exit();
        }
    }

    private void showUpdateErrorDialog() {
        final CustomsizeDialog customsizeDialog = new CustomsizeDialog(this);
        customsizeDialog.setTitle(R.string.dialog_title_alert);
        customsizeDialog.setContent(getString(R.string.activity_hub_setting_firmware_upgrade_failed));
        customsizeDialog.setCancelable(false);
        customsizeDialog.setNegativeButton(R.string.cancel, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customsizeDialog.dismiss();
            }
        });
        customsizeDialog.setCanceledOnTouchOutside(false);
        customsizeDialog.show();
    }

    @Override
    public void onBackPressed() {
        exit();
    }

    @Override
    public void onNetworkInteractAllOver(int ret) {

    }

    @Override
    public void onNetworkInteractOver(int ret, String flag, Object object) {
        if (flag.equals(networkInteractUtil.SYNCMODIFYBOX)) {
            if (ret == 0) {
                modifyPasswordDialog.dismiss();
                showToast(getString(R.string.modify_success));
                finish();
                overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            } else if (ret == -1) {
                hideLoadingDialog();
                modifyPasswordDialog.dismiss();
                showToast(getString(R.string.modify_failed));
                showToast(getString(R.string.activity_box_modify_modify_failed));
            }
        }
    }

    class UiHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EXIT:
                    exit();
                    break;
                case ALLCHANGE:
                    tvStorage.setText(String.format(getString(R.string.activity_hub_setting_format_storage), (Integer.valueOf(storageAll) - Integer.valueOf(storageUsed))));
                    tvFlash.setText(String.format(getString(R.string.activity_hub_setting_format_storage), (Integer.valueOf(flashAll) - Integer.valueOf(flashUsed))));
                    String version[] = hubVersion.split("\\.");
                    String versionStr = "V" + version[0];
                    tvVersionName.setText(versionStr);
                    break;
                case SHOWPROGRESSDIALOG:
                    alert();
                    break;
                case CHANGEPROGRESSDIALOG:
                    int progress = msg.arg1;
                    mProgress.setProgress(progress);
                    break;
                case MD5MATCHRESULT:
                    String result = (String) msg.obj;
                    myDialog.dismiss();
                    if (result.equals("matched")) {
                        showUpdateConFirmDialog();
                    } else {
                        showUpdateErrorDialog();
                    }
                    break;
            }
        }
    }
}
